package weaver.splittable.task;

import org.apache.log4j.Logger;
import weaver.conn.ConnectionPool;
import weaver.conn.WeaverConnection;
import weaver.conn.WeaverStatement;
import weaver.splittable.STConfig;
import weaver.splittable.biz.workflow.STWorkflowInterceptor;
import weaver.splittable.db.STDBConstants;
import weaver.splittable.db.STDBSupport;
import weaver.splittable.modal.ThresholdInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

public class STThresholdTask extends TimerTask {
    private static final Logger LOGGER = Logger.getLogger(STConfig.LOGGER_NAME);
    @Override
    public void run() {
        try {
            Date nowDate = new Date();
            DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate_string = dFormat.format(nowDate);
            ThresholdInfo workflowThresholdInfo = queryThreshold("workflow");
            ThresholdInfo formmodelThresholdInfo = queryThreshold("formmodel");
            if (workflowThresholdInfo!=null&&(!workflowThresholdInfo.getCreatedate_threshold().equals(STWorkflowInterceptor.getTimeThreshold())||!workflowThresholdInfo.getRequestid_threshold().equals(STWorkflowInterceptor.getRequestIdThreshold()))){
                STWorkflowInterceptor.setRequestIdThreshold(workflowThresholdInfo.getRequestid_threshold());
                STWorkflowInterceptor.setTimeThreshold(workflowThresholdInfo.getCreatedate_threshold());
            }
            //TODO 如果有其它模块记得添加阈值更新
        }catch (Exception e){
            LOGGER.error("STThresholdTask sync Threshold error:", e);
        }
    }

    private ThresholdInfo queryThreshold(String module){
        WeaverConnection connection = null;
        WeaverStatement statement = null;
        try {
            connection = ConnectionPool.getInstance().getConnection();
            statement = (WeaverStatement) connection.createStatement();
            //查询是否数据已存在
            String querySql = "";
            if (STConfig.DB_TYPE.equals(STDBSupport.SQLSERVER)) {
                querySql = "SELECT  * FROM " + STDBConstants.DATA_THRESHOLD_TABLE_NAME + " WHERE module='"+module+"' ";
            } else if (STConfig.DB_TYPE.equals(STDBSupport.MYSQL)) {
                querySql = "SELECT * FROM " + STDBConstants.DATA_THRESHOLD_TABLE_NAME + " WHERE module='"+module+"' ";
            } else if (STConfig.DB_TYPE.equals(STDBSupport.ORACLE)) {
                querySql = "SELECT  * FROM " + STDBConstants.DATA_THRESHOLD_TABLE_NAME + " WHERE module='"+module+"' ";
            }
            ResultSet resultSet = statement.originExecuteQuery(querySql);
            while(resultSet.next()){
                Integer requestid_threshold = resultSet.getInt("requestid_threshold");
                String createdate_threshold = resultSet.getString("createdate_threshold");
                String updatetime = resultSet.getString("updatetime");
                return new ThresholdInfo(requestid_threshold,createdate_threshold,updatetime,module);
            }
            return null;
        }catch (Exception e){
            LOGGER.error("STThresholdTask  error:", e);
        }finally {
            if (statement != null) {
                statement.closeNoException();
            }
            if (connection != null) {
                try {
                    connection.setAutoCommit(true);
                } catch (SQLException e) {
                    LOGGER.error("STThresholdTask  setAutoCommit true error:", e);
                }
                ConnectionPool.getInstance().returnConnection(connection);
            }
        }
        return null;
    }
}
